package com.dgut.hisystem.controller;

import com.dgut.hisystem.entity.Charge;
import com.dgut.hisystem.entity.Patient;
import com.dgut.hisystem.entity.VO.BalanceVO;
import com.dgut.hisystem.entity.VO.BillVO;
import com.dgut.hisystem.entity.VO.ChargeVO;
import com.dgut.hisystem.entity.VO.DailyVO;
import com.dgut.hisystem.service.ChargeService;
import com.dgut.hisystem.service.PatientService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/charge")
public class ChargeController {
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private PatientService patientService;

    @RequestMapping("/insert")
    void insert(@RequestBody Charge charge){
        chargeService.insert(charge);
    }

    @RequestMapping("/updateCharge")
    void updateCharge(@RequestBody @RequestParam("sf_id") int sf_id){
        chargeService.updateCharge(sf_id);
        chargeService.pay(sf_id);
    }

    @RequestMapping("/updateRefund")
    void updateRefund(@RequestBody @RequestParam("sf_id") int sf_id){
        chargeService.updateRefund(sf_id);
        chargeService.back(sf_id);
    }

    @RequestMapping("/updateBalance")
    void updateBalance(@RequestBody @RequestParam("card") long card , @RequestBody @RequestParam("money") double money){
        Patient patient = patientService.selectID(card);
        money = patient.getBr_balance() + money;
        patientService.updateBalance(money,patient);
    }

    @RequestMapping("/returnBalance")
    void returnBalance(@RequestBody @RequestParam("card") long card){
        Patient patient = patientService.selectID(card);
        patientService.updateBalance(0,patient);
    }

    @RequestMapping("/selectRefund")
    List<ChargeVO> selectRefund(HttpServletRequest request){
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return chargeService.selectRefund();
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return chargeService.selectRefundByCard(card);
        }
    }

    @RequestMapping("/selectDaily")
    List<DailyVO> selectDaily(HttpServletRequest request){
        Timestamp c = new Timestamp(Calendar.getInstance().get(Calendar.YEAR) - 1900 ,Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DATE),0,0,0,0);
        Timestamp d = new Timestamp(Calendar.getInstance().get(Calendar.YEAR) - 1900 ,Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DATE),23,59,59,0);
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return chargeService.selectDaily(c,d);
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return chargeService.selectDailyByCard(card,c,d);
        }
    }

    @RequestMapping("/selectCharge")
    List<ChargeVO> selectCharge(HttpServletRequest request){
        if(request.getParameter("card") == null|| request.getParameter("card").equals("")){
            return chargeService.selectCharge();
        }
        else{
            long card = Long.parseLong(request.getParameter("card"));
            return chargeService.selectChargeByCard(card);
        }
    }

    @RequestMapping("/getBalance")
    BalanceVO getBalance(HttpServletRequest request){
        if(request.getParameter("card")==null){
            int sf_id = Integer.parseInt(request.getParameter("sf_id"));
            Charge charge = chargeService.getChargeById(sf_id);
            BalanceVO balanceVO = chargeService.getBalance(charge.getBr_card());
            BeanUtils.copyProperties(charge,balanceVO);
            return balanceVO;
        }
        else {
            long card = Long.parseLong(request.getParameter("card"));
            return chargeService.getBalance(card);
        }
    }

    @RequestMapping("/getBill")
    List<BillVO> getBill(@RequestBody @RequestParam("sf_id") int sf_id){
        return chargeService.getBill(sf_id);
    }
}
